package com.baikeyang.service.activity.impl;

import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.CommonConstants;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.ActivityFileLibrary;
import com.baikeyang.common.model.ActivityPhotoAlbum;
import com.baikeyang.common.model.ActivityPhotoAlbumFile;
import com.baikeyang.common.util.shiro.ShiroUtil;
import com.baikeyang.mapper.ActivityPhotoAlbumFileMapper;
import com.baikeyang.mapper.ActivityPhotoAlbumMapper;
import com.baikeyang.service.activity.IActivityFileLibraryService;
import com.baikeyang.service.activity.IActivityPhotoAlbumService;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 活动照片相册 服务实现类
 * </p>
 *
 * @author BaiKeyang
 * @since 2019-05-21
 */
@Service
public class IActivityPhotoAlbumServiceImpl extends SuperServiceImpl<ActivityPhotoAlbumMapper, ActivityPhotoAlbum> implements IActivityPhotoAlbumService {

    @Autowired
    private ActivityPhotoAlbumMapper albumMapper;

    @Autowired
    private IActivityFileLibraryService fileLibraryService;

    @Override
    public ResponseResult saveActivityPhotoAlbum(ActivityPhotoAlbum album) {
        album.setCreateTime( new Date() );
        album.setCreateUser( ShiroUtil.getCurrentUserId() );
        album.setUpdateTime( new Date() );
        album.setUpdateUser( ShiroUtil.getCurrentUserId() );
        album.setState( CommonConstants.DATA_STATUS_VALID );
        int flag = albumMapper.insert(album);
        if ( flag > 0 ) {
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult updateActivityPhotoAlbum(ActivityPhotoAlbum album) {
        album.setUpdateTime( new Date() );
        album.setUpdateUser( ShiroUtil.getCurrentUserId() );
        int flag = albumMapper.updateById(album);
        if ( flag > 0 ) {
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult deleteActivityPhotoAlbum(int id) {
        ActivityPhotoAlbum album = albumMapper.selectById(id);
        if ( null != album ) {
            album.setState( CommonConstants.DATA_STATUS_DELETE );
            int flag = albumMapper.updateById(album);
            if ( flag > 0 ) {
                return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public IPage<ActivityPhotoAlbum> loadActivityPhotoAlbumPage(Page<ActivityPhotoAlbum> pageParam) {
        QueryWrapper<ActivityPhotoAlbum> queryWrapper = new QueryWrapper<ActivityPhotoAlbum>();
        queryWrapper.orderByDesc("CREATE_TIME");
        return albumMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public List<ActivityFileLibrary> loadActivityFileLibByAlbum(int id) {
        return fileLibraryService.loadActivityFileLibraryByAlbum(id);
    }
}
